import moment from 'moment'


export function dateYYYYMMDDhhmmss(str) {
  if (!str) return ""
  return moment(str).format('YYYY-MM-DD hh:mm:ss')
}



export function dateYYYYMMDD(str) {
  if (!str) return ""
  return moment(str).format('YYYY-MM-DD')
}



export function timeMMDDSS(time) {
  if (!time) return ""
  var hh;
  var mm;
  var ss;
  //传入的时间为空或小于0
  if (time == null || time < 0) {
    return;
  }
  //得到小时
  hh = time / 3600 | 0;
  time = parseInt(time) - hh * 3600;
  if (parseInt(hh) < 10) {
    hh = "0" + hh;
  }
  //得到分
  mm = time / 60 | 0;
  //得到秒
  ss = parseInt(time) - mm * 60;
  if (parseInt(mm) < 10) {
    mm = "0" + mm;
  }
  if (ss < 10) {
    ss = "0" + ss;
  }
  return hh + ":" + mm + ":" + ss;
}


/**
 * 返回年月日
 * @export
 * @param {Date} date
 * @param {string} [splitor='-']
 * @returns
 */
export function getDate(date, splitor = '-') {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return `${year}${splitor}${addZeroPrefix(month)}${splitor}${addZeroPrefix(day)}`
}

/**
 * 返回时分秒/时分
 * @export
 * @param {*} date
 * @param {boolean} [withSecond=false]
 * @returns
 */
export function getTime(date, withSecond = false) {
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  return withSecond ? `${addZeroPrefix(hour)}:${addZeroPrefix(minute)}:${addZeroPrefix(second)}` : `${hour}:${addZeroPrefix(minute)}`
}

export function getFullDate(date) {
  return `${getDate(date)} ${getTime(date)}`
}

export function isToday(date) {
  return date.toDateString() === new Date().toDateString()
}


/**
 * 个位数，加0前缀
 * @param {*} number
 * @returns
 */
function addZeroPrefix(number) {
  return number < 10 ? `0${number}` : number
}

/*
* @return str  多少天 hh:mm:ss
* @return false 已经结束
* */
export function dateDownTime(targetDate) {
  // 获取本地当前时间，截止时间 - 当前时间 = 倒计时时间
  if (!targetDate) return false
  var Today = new Date();
  var endDate = new Date(targetDate);
  var leftTime = endDate.getTime() - Today.getTime();
  var leftSecond = parseInt(leftTime / 1000);
  var Day = Math.floor(leftSecond / (60 * 60 * 24));
  var Hour = Math.floor((leftSecond - Day * 24 * 60 * 60) / 3600);
  var Minute = Math.floor((leftSecond - Day * 24 * 60 * 60 - Hour * 3600) / 60);
  var Second = Math.floor(leftSecond - Day * 24 * 60 * 60 - Hour * 3600 - Minute * 60);
  if (Day <= 0 & Hour <= 0 & Minute <= 0 & Second <= 0) {
    return false
  }
  var str = ""
  if (Day > 0) {
    str+=`${Day}天`
  }
  str+=`${fix10(Hour)}:${fix10(Minute)}:${fix10(Second)}`
  return str

}

function fix10(str) {
  var str = parseInt(str)
  if (str < 10) {
    return "0" + str
  } else {
    return str
  }
}

//将秒转化为时分秒
export function formateSeconds(endTime) {
  let secondTime = parseInt(endTime)//将传入的秒的值转化为Number
  let min = 0// 初始化分
  let h = 0// 初始化小时
  let result = ''
  if (secondTime > 60) {//如果秒数大于60，将秒数转换成整数
    min = parseInt(secondTime / 60)//获取分钟，除以60取整数，得到整数分钟
    secondTime = parseInt(secondTime % 60)//获取秒数，秒数取佘，得到整数秒数
    if (min > 60) {//如果分钟大于60，将分钟转换成小时
      h = parseInt(min / 60)//获取小时，获取分钟除以60，得到整数小时
      min = parseInt(min % 60) //获取小时后取佘的分，获取分钟除以60取佘的分
    }
  }
  result = `${h.toString().padStart(2, '0')}:${min.toString().padStart(2, '0')}:${secondTime.toString().padStart(2, '0')}`
  return result
}


export function isBeforeDate(before, after) {
  return moment(before).diff(moment(after), 's') < 0
}

